import os,re
from flask import current_app
import time

class executor_cmd():
    def __init__(self):
        self.PIPE_USER=current_app.config.get("PIPE_USER")
        self.PASSWORD=current_app.config.get("PASSWORD")
        self.PREFIX=current_app.config.get("PATH_PREFIX")
        self.HOST_IP=current_app.config.get("HOST_IP")
    
    def sshpass_ttcmd(self,cmd):
        cmd=cmd.rstrip()
        cmd = '''sshpass -p {password} ssh -tt -o StrictHostKeyChecking=no -p 22 {user}@{hostIP} \"{cmd}\"'''.format(
                user=self.PIPE_USER,
                password=self.PASSWORD,
                prefix=self.PREFIX,
                hostIP=self.HOST_IP,
                cmd=cmd
            )
        os.popen(cmd)
        return cmd,202
    
    def sshpass_cmd(self,cmd):
        cmd=cmd.rstrip()
        cmd = '''sshpass -p {password} ssh -o StrictHostKeyChecking=no -p 22 {user}@{hostIP} \"{cmd}\"'''.format(
                user=self.PIPE_USER,
                password=self.PASSWORD,
                prefix=self.PREFIX,
                hostIP=self.HOST_IP,
                cmd=cmd
            )
        os.popen(cmd)
        return cmd,202
    
    def sshpass_return(self,cmd):
        cmd=cmd.rstrip()
        cmd = '''sshpass -p {password} ssh -o StrictHostKeyChecking=no -p 22 {user}@{hostIP} {cmd}'''.format(
                user=self.PIPE_USER,
                password=self.PASSWORD,
                prefix=self.PREFIX,
                hostIP=self.HOST_IP,
                cmd=cmd
            )
        os.popen(cmd).read()
        return cmd,202